home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / 32SNIPIT.PAK / IDXEXPR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  11.2 KB  |  326 lines

  1. // BDE32 3.x - (C) Copyright 1996 by Borland International
  2.  
  3. // idxexpr.c
  4. #include "snipit.h"
  5.  
  6. #define NAMELEN  30 // Length of the name field
  7.  
  8. static const char szTblName[] = "order";
  9. static const char szMasterTblName[] = "cust";
  10.  
  11. static const char szTblType[] = szDBASE;
  12.  
  13. // Key expression used to link the detail table.
  14. static SNIPFAR char szMstrExp[] = "CITY - (\", \" + STATE_PROV)";
  15.  
  16. // Field descriptor used in creating a table.
  17. static SNIPFAR FLDDesc fldDesc[] = {
  18.     { // Field 1 - Customer Number
  19.         1,            // Field number
  20.         "CITY_STATE", // Field name
  21.         fldZSTRING,   // Field type
  22.         fldUNKNOWN,   // Field subtype
  23.         35,           // Field size ( 1 or 0, except
  24.                       // BLOb or CHAR field )
  25.         0,            // Decimal places ( 0 )
  26.                       // computed
  27.         0,            // Offset in record ( 0 )
  28.         0,            // Length in bytes  ( 0 )
  29.         0,            // For Null bits    ( 0 )
  30.         fldvNOCHECKS, // Validity checks   ( 0 )
  31.         fldrREADWRITE // Rights
  32.     },
  33.     { // Field 2 - Name
  34.         2, "NAME", fldZSTRING, fldUNKNOWN,
  35.         NAMELEN, 0, 0, 0, 0,
  36.         fldvNOCHECKS, fldrREADWRITE
  37.     },
  38.     { // Field 3 - Phone
  39.         3, "Phone", fldZSTRING, fldUNKNOWN,
  40.         15, 0, 0, 0, 0, fldvNOCHECKS, fldrREADWRITE
  41.     }};  // Array of field descriptors
  42.  
  43. // Index descriptor - describes the Index associated with the
  44. // detail table.
  45. static SNIPFAR IDXDesc idxDesc[] = {
  46.     {  // Production Index - expression index
  47.         "order.mdx",  // Name
  48.         NULL,         // Number
  49.         {"CITY_STATE"}, // Tag name ( for dBASE )
  50.         { NULL },     // Optional format ( BTREE,
  51.                       // HASH, etc )
  52.         FALSE,        // Primary?
  53.         FALSE,        // Unique?
  54.         FALSE,        // Descending?
  55.         TRUE,         // Maintained?
  56.         FALSE,        // SubSet?
  57.         FALSE,        // Expression index?
  58.         NULL,         // for QBE only
  59.         1,            // Fields in key
  60.         1,            // Length in bytes
  61.         FALSE,        // Index out of date?
  62.         0,            // Key type of expression
  63.         { 1 },        // Array of field numbers
  64.         { NULL },     // Key expression
  65.         { NULL },     // Key condition
  66.         FALSE,        // Case insensitive
  67.         0,            // Block size in bytes
  68.         0             // Restructure number
  69.     }};
  70.  
  71. static DBIResult InitTable(phDBIDb phDb);
  72. static DBIResult AddRecord(phDBICur phCur, pCHAR pszCityState, pCHAR pszName,
  73.                            pCHAR pszPhone);
  74.  
  75. //=====================================================================
  76. //  Function:
  77. //          IndexExpr();
  78. //
  79. //  Description:
  80. //          This example shows how to create expression indexes and
  81. //          link two tables together based upon the expression index
  82. //          (which is similar to Set Relation to... in the dBASE language).
  83. //          When the link is established, you will only be able to see a
  84. //          subset of the records in the Detail table which correspond
  85. //          to the current record of the Master table.
  86. //=====================================================================
  87. void
  88. IndexExpr (void)
  89. {
  90.     hDBIDb      hDb = 0;        // Database handle
  91.     hDBICur     hCur = 0;       // Cursor handle to the detail table.
  92.     hDBICur     hMCur;          // Cursor handle to the master table.
  93.     UINT16      i;              // For looping.
  94.     DBIResult   rslt;           // Return value from IDAPI functions
  95.  
  96.     Screen("*** dBASE Expression Index Example ***\r\n");
  97.  
  98.     BREAK_IN_DEBUGGER();
  99.  
  100.     Screen("    Initializing IDAPI...");
  101.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  102.     {
  103.  
  104.         Screen("\r\n*** End of Example ***");
  105.         return;
  106.     }
  107.  
  108.     Screen("    Setting the database directory...");
  109.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  110.     ChkRslt(rslt, "SetDirectory");
  111.  
  112.     // Create the table and insert four records.
  113.     if (InitTable(&hDb))
  114.     {
  115.         CloseDbAndExit(&hDb);
  116.         Screen("\r\n*** End of Example ***");
  117.         return;
  118.     }
  119.  
  120.     Screen("    Open the %s table as the detail table (must be opened in"
  121.            " shared mode)...", szTblName);
  122.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  123.                         idxDesc[0].szName, idxDesc[0].szTagName, NULL, dbiREADWRITE, dbiOPENSHARED,
  124.                         xltFIELD, FALSE, NULL, &hCur);
  125.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  126.     {
  127.         CloseDbAndExit(&hDb);
  128.         Screen("\r\n*** End of Example ***");
  129.         return;
  130.     }
  131.  
  132.     Screen("    Open the %s table, which is used as the master table...",
  133.            szMasterTblName);
  134.     rslt = DbiOpenTable(hDb, (pCHAR) szMasterTblName, (pCHAR) szTblType,
  135.                         NULL, NULL, NULL,  dbiREADWRITE, dbiOPENSHARED,
  136.                         xltFIELD, FALSE, NULL, &hMCur);
  137.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  138.     {
  139.         CloseDbAndExit(&hDb);
  140.         Screen("\r\n*** End of Example ***");
  141.         return;
  142.     }
  143.  
  144.     // Go to the top of the master table which is ordered by cust_no.
  145.     rslt = DbiSetToBegin(hMCur);
  146.     ChkRslt(rslt, "SetToBegin");
  147.  
  148.     // Put the Cust table into link mode.
  149.     rslt = DbiBeginLinkMode(&hMCur);
  150.     ChkRslt(rslt, "BeginLinkMode");
  151.  
  152.     // Put the Orders table into link mode.
  153.     rslt = DbiBeginLinkMode(&hCur);
  154.     ChkRslt(rslt, "BeginLinkMode");
  155.  
  156.     // Now link the two cursors/tables together.
  157.     Screen("    Link the two tables togther");
  158.  
  159.     rslt = DbiLinkDetailToExp(hMCur, hCur, 0, (pCHAR) szMstrExp);
  160.     ChkRslt(rslt, "LinkDetailToExp");
  161.  
  162.     // The detail table has matching data only for the first two records 
  163.     //   of the master table.  
  164.     for (i = 0; i < 3; i++)
  165.     {
  166.         // Display the master record.
  167.         Screen("\r\n        This is the master record");
  168.         DisplayTable(hMCur, 1);
  169.  
  170.         // Display all the detail records.
  171.         Screen("\r\n        This is the detail record(s)");
  172.         DisplayTable(hCur, 0);
  173.     }
  174.  
  175.     Screen("\r\n        There are no records in the detail table for the"
  176.            "\r\n        third record in the master table.  Therefore, it"
  177.            "\r\n        should come up blank");
  178.  
  179.     Screen("\r\n    End the link...");
  180.     rslt = DbiUnlinkDetail(hCur);
  181.     ChkRslt(rslt, "EndLinkMode");
  182.  
  183.     // Take the Customer table out of link mode.
  184.     rslt = DbiEndLinkMode(&hMCur);
  185.     ChkRslt(rslt, "EndLinkMode");
  186.  
  187.     // Take the Orders table out of link mode.
  188.     rslt = DbiEndLinkMode(&hCur);
  189.     ChkRslt(rslt, "EndLinkMode");
  190.  
  191.     Screen("    Close the %s table...", szMasterTblName);
  192.     rslt = DbiCloseCursor(&hMCur);
  193.     ChkRslt(rslt, "CloseCursor");
  194.  
  195.     Screen("    Close the %s table...", szTblName);
  196.     rslt = DbiCloseCursor(&hCur);
  197.     ChkRslt(rslt, "CloseCursor");
  198.  
  199.     Screen("    Delete the %s table...", szTblName);
  200.     rslt = DbiDeleteTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType);
  201.     ChkRslt(rslt, "DeleteTable");
  202.  
  203.     Screen("    Close the database and exit IDAPI...");
  204.     CloseDbAndExit(&hDb);
  205.  
  206.     Screen("\r\n*** End of Example ***");
  207. }
  208.  
  209. //=====================================================================
  210. //  Function:
  211. //          InitTable(phDb);
  212. //
  213. //  Input:  phDb    - Pointer to the database handle.
  214. //
  215. //  Return: result of the table initialization.
  216. //
  217. //  Desc:   This function will create a table and fill the table
  218. //          with a number of records.  The function uses
  219. //          another function called AddRecord which adds records to
  220. //          the table.
  221. //=====================================================================
  222. DBIResult
  223. InitTable (phDBIDb phDb)
  224. {
  225.     DBIResult   rslt;               // Value returned from IDAPI functions
  226.     hDBICur     hCur;               // Cursor handle for the table that is
  227.                                     //   created.
  228.     CRTblDesc   crTblDsc;           // Table descriptor
  229.     BOOL        bOverWrite = TRUE;  // Overwrite, yes/no flag
  230.  
  231.     // The number of fields in the table.
  232.     const UINT16 uNumFields = sizeof(fldDesc) / sizeof (fldDesc[0]);
  233.  
  234.     // Number of indexes to be created when the table is created.
  235.     const UINT16 uNumIndexes = sizeof(idxDesc) / sizeof(idxDesc[0]);
  236.  
  237.     // Initialize the Table Create descriptor.
  238.     memset(&crTblDsc, 0, sizeof(CRTblDesc));
  239.     strcpy(crTblDsc.szTblName, szTblName);
  240.     strcpy(crTblDsc.szTblType, szTblType);
  241.     crTblDsc.iFldCount     = uNumFields;
  242.     crTblDsc.pfldDesc      = fldDesc;
  243.     crTblDsc.iIdxCount     = uNumIndexes;
  244.     crTblDsc.pidxDesc      = idxDesc;
  245.  
  246.     Screen("    Creating the table and indexes...");
  247.     rslt = DbiCreateTable(*phDb, bOverWrite, &crTblDsc);
  248.     if (ChkRslt(rslt, "CreateTable") != DBIERR_NONE)
  249.     {
  250.         return rslt;
  251.     }
  252.  
  253.     rslt = DbiOpenTable(*phDb, (pCHAR) szTblName, (pCHAR) szTblType,
  254.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  255.                         xltFIELD, FALSE, NULL, &hCur);
  256.     ChkRslt(rslt, "OpenTable");
  257.  
  258.     Screen("    Adding records to the table...");
  259.     AddRecord(&hCur, "Kapaa Kauai, HI", "Kapaa Dive Shoppe",
  260.               "(408)-321-1729");
  261.     AddRecord(&hCur, "Kapaa Kauai, HI", "Kapaa Dive Shoppe",
  262.               "(408)-321-1729");
  263.     AddRecord(&hCur, "Freeport, ", "Unisco", "(415)-567-1799");
  264.     AddRecord(&hCur, "Freeport, ", "Divers", "(415)-365-6159");
  265.  
  266.     rslt = DbiCloseCursor(&hCur);
  267.     ChkRslt(rslt, "CloseCursor");
  268.  
  269.     return rslt;
  270. }
  271.  
  272. //=====================================================================
  273. //  Function:
  274. //          AddRecord(phCur, pszCityState, pszName, pszPhone);
  275. //
  276. //  Input:  phCur           - Pointer to the cursor handle
  277. //          pszCityState    - City and State
  278. //          pszName         - Customer Name
  279. //          pszPhone        - Customer phone number
  280. //
  281. //  Return: Result of adding the record to the table
  282. //
  283. //  Desc:   This function will add a record to the given table.
  284. //=====================================================================
  285. DBIResult
  286. AddRecord (phDBICur hCur, pCHAR pszCityState, pCHAR pszName, pCHAR pszPhone)
  287. {
  288.     DBIResult   rslt;           // Value returned from IDAPI functions
  289.     CURProps    TblProps;       // Table Properties
  290.     pBYTE       pRecBuf;        // Record Buffer
  291.  
  292.     //  Allocate a record buffer.
  293.     rslt = DbiGetCursorProps(*hCur, &TblProps);
  294.     ChkRslt(rslt, "GetCursorProps");
  295.  
  296.     pRecBuf = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  297.     if (pRecBuf == NULL)
  298.     {
  299.         return DBIERR_NOMEMORY;
  300.     }
  301.  
  302.     // Start with a clean record buffer.
  303.     rslt = DbiInitRecord(*hCur, pRecBuf);
  304.     ChkRslt(rslt, "InitRecord");
  305.  
  306.     // City_state.
  307.     rslt = DbiPutField(*hCur, 1, pRecBuf, (pBYTE)pszCityState);
  308.     ChkRslt(rslt, "PutField");
  309.  
  310.     // Customer Name.
  311.     rslt = DbiPutField(*hCur, 2, pRecBuf, (pBYTE) pszName);
  312.     ChkRslt(rslt, "PutField");
  313.  
  314.     // Customer Phone Number.
  315.     rslt = DbiPutField(*hCur, 3, pRecBuf, (pBYTE) pszPhone);
  316.     ChkRslt(rslt, "PutField");
  317.  
  318.     // Now insert the record.
  319.     rslt = DbiInsertRecord(*hCur, dbiNOLOCK, pRecBuf);
  320.     ChkRslt(rslt, "InsertRecord");
  321.  
  322.     free(pRecBuf);
  323.  
  324.     return rslt;
  325. }
  326.